Skip to content

Fix buffer overflow in --residual-gnuplot analysis - #959

Open
Perennos wants to merge 1 commit into
xiph:masterfrom
Perennos:fix-analyze-bucket-overflow
Open

Perennos wants to merge 1 commit into
xiph:masterfrom
Perennos:fix-analyze-bucket-overflow

Conversation

@Perennos

Copy link
Copy Markdown

Fixes #887

Problem

flac --analyze --residual-gnuplot tracks a residual-value histogram in all_ (src/flac/analyze.c) using a fixed array of FLAC__MAX_BLOCK_SIZE buckets, initially one per distinct residual. When an input contains more than FLAC__MAX_BLOCK_SIZE distinct residuals, writes go past the end of the bucket array, overwriting the adjacent peak_index and nbuckets fields and causing the crash reported by ASan in the issue.

Fix

As suggested in the issue, coarsen the histogram when the bucket array fills: double the bucket width and merge buckets that now overlap. Output is unchanged for inputs that fit within the array, and the mean and standard deviation continue to be calculated from the exact residual values.

Testing

Built with ASan on Linux (gcc 13, CMake).

  • Issue PoC (24-bit mono noise, -0): before the change, --analyze --residual-gnuplot fails with an ASan SEGV in update_stats (analyze.c:207). After: exit 0, no ASan report.
  • test/test_flac.sh: added a step that analyzes an 80000-sample file with more than 65535 distinct residuals and verifies that the overall distribution counts every sample. Before the fix, the flac test fails (counts 12359 samples instead of 80000). After: passes.
  • ctest -R "generate_streams|flac": 2/2 pass.

AI-assisted: drafted with an AI coding assistant; reproduced and tested locally before submitting.

The overall residual distribution (all_) in src/flac/analyze.c has a
fixed array of FLAC__MAX_BLOCK_SIZE buckets, one per distinct residual
value. A long enough file with enough distinct residuals wrote past the
end of the array, corrupting the fields that follow it (or crashing).

When the array is full, make the buckets twice as wide and merge the
buckets that now overlap, as suggested in the issue. Files that fit
keep exact, one-residual-per-bucket output.

Add a test to test_flac.sh that analyzes a file with more distinct
residuals than buckets and checks that every sample is counted.

Fixes xiph#887
@Perennos

Copy link
Copy Markdown
Author

Hi, no rush at all; whenever you have a moment, a review would be appreciated. This fixes the --residual-gnuplot buffer overflow reported in #887 using the coarser-buckets suggestion from that issue, and adds a regression check in test_flac.sh that fails before the fix and passes after. If you'd prefer a different approach, I'm happy to rework it. Thank you for maintaining FLAC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Memory error in flac analyze.c

1 participant